package org.ands.xml;

import javax.xml.namespace.NamespaceContext;

import org.w3c.dom.Document;


public class DOMHelperFromFile extends DOMHelper{
	
	/**
	 * Instantiates a new dOM helper.
	 * 
	 * @param document
	 *            the document
	 * @throws Exception
	 *             the exception
	 */
	public DOMHelperFromFile(final String filename, final NamespaceContext nsContext) throws Exception {
		
		_document = constructDocument(filename);
		_nsContext = nsContext;
	} 
	
	/**
	 * Instantiates a new dOM helper.
	 * 
	 * @param document
	 *            the document
	 * @throws Exception
	 *             the exception
	 */
	public DOMHelperFromFile(final String filename) throws Exception {
		
		_document = constructDocument(filename);
		assert(_document != null);
		_nsContext = new UniversalNamespaceResolver(_document);
		assert(_nsContext != null);
		
	} 
	
	private Document constructDocument(final String filename) throws Exception{
		
		if((filename == null) || (filename.isEmpty())){
			throw new Exception("Requires valid filename");
		}
		
		Document document = documentBuilderInstance().parse(filename);
		
		assert(document != null);
		if(document == null){
			throw new Exception("Failed to construct Document from filename "+filename);
		}
		
		assert(document.hasChildNodes());
		
		assert(document != null);
		return document;
	}
	

}
